package cn.player.media.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdpt<T> extends BaseAdapter {

    public Context mContext;
    public List<T> mData;
    public List<Integer> mViewIds;
    public int layoutResource;
    public ViewHolder viewHolder;

    public BaseAdpt() {
        super();
        // TODO Auto-generated constructor stub
    }
 
	/*public BaseAdpt(Context context, List<T> data) {
        super();
		this.mContext = context;
		this.mData = data;
	}*/

    /**
     * @param mContext       Context
     * @param mData          列表数据集合
     * @param layoutResource itemView资源
     */
    public BaseAdpt(Context mContext, List<T> mData, int layoutResource) {
        super();
        this.mContext = mContext;
        this.mData = mData;
        this.layoutResource = layoutResource;
        mViewIds = new ArrayList<Integer>();
    }

    public void setData(List<T> mData) {
        this.mData = mData;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mData != null ? mData.get(position) : position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /**
     * 设置itemView资源
     *
     * @param layoutResource
     */
    public void setItemView(int layoutResource) {
        this.layoutResource = layoutResource;
    }

    /**
     * 子类在此方法中添加itemview中需要操作的视图id
     *
     */
    protected abstract void addViewId();

    /**
     * 从viewHolder取出view并更新
     *
     * @param position
     */
    protected abstract void updateItemViews(int position);

    protected void animationShow(int position, View convertView) {
    }

    ;


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = View.inflate(mContext, layoutResource, null);
            viewHolder = new ViewHolder(convertView);
            addViewId();
            if (mViewIds != null && mViewIds.size() > 0) {
                for (Integer id : mViewIds) {
                    viewHolder.addView(id);
                }
            }
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (mData != null && mData.size() > 0) {
            updateItemViews(position);
        }

        animationShow(position, convertView);

        return convertView;
    }

    public static class ViewHolder {
        private View convertView;

        public ViewHolder(View convertView) {
            this.convertView = convertView;
        }

        public SparseArray<View> views = new SparseArray<View>();

        public void addView(int id) {
            views.put(id, convertView.findViewById(id));
        }

        public View getView(int id) {
            return views.get(id);
        }

        public View getConvertView() {
            return convertView;
        }
    }

}
